#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1e3+10;
const int MAXE = 2e5+10;

typedef struct {int v,nxt;}edge;

edge e[MAXE];
int h[MAXN],dis[MAXN],blo[MAXN],col[MAXN],ans[MAXN],cnt,tot,n,m;
bool fla;

void link(int x,int y){e[++cnt] = (edge){y,h[x]};h[x] = cnt;}

void dfs(int x,int co){
	blo[x] = tot; col[x] = co;
	for(int i = h[x];i;i = e[i].nxt){
		int y = e[i].v;
		if(!col[y]) dfs(y,3-co);
		else if(col[y] == col[x]) return (void)(fla = 1);
	}
	return ;
}

void bfs(int x){
	memset(dis,-1,sizeof(dis));
	queue<int> q;
	dis[x] = 0; q.push(x);
	while(q.size()){
		x = q.front();q.pop();
		for(int i = h[x];i;i = e[i].nxt){
			int y = e[i].v;
			if(dis[y] == -1) {
				dis[y] = dis[x] + 1;
				q.push(y);
			}
		}
	}
}

int main (){
	scanf("%d %d",&n,&m);
	for(int i = 1;i <= m;i++){
		int x,y;scanf("%d %d",&x,&y);
		link(x,y);link(y,x);
	}
	for(int i = 1;i <= n;i++){
		if(blo[i]) continue;
		tot++;
		dfs(i,1);
		if(fla){
			puts("-1");
			return 0;
		}
	}
	for(int i = 1;i <= n;i++){
		bfs(i);
		for(int j = 1;j <= n;j++)
			ans[blo[i]] = max(ans[blo[i]],dis[j]);
	}
	int ANS = 0;
	for(int i = 1;i <= tot;i++) ANS += ans[i];
	printf("%d\n",ANS);
	return 0;
}
